
%include "&studyroot\work\programs\macros\template\tdd_formats.sas";

%get_supp(datain=eg);

Proc sort data =eg_supp out = seg;
   by usubjid visitnum visit egdtc;* egpos;
run;

data info;
   length usubjid $50. studyid $25.;
   merge seg(in = a) adam.adsl;
   by usubjid;
   if a;
   if %ifexist(in=eg_supp,var=EGCOM) = 0 then do; EGCOM = ''; end;
run;

Proc freq data =info ;
   tables egtest*egtestcd visitnum*visit/ list missing nocum nopercent;
run;

%copyMe(eg,in=eg,lib=sdtm,fmt=3);
Proc freq data = info;
   tables egstresc/ list missing nocum nopercent;
run;
Proc sort data =info ;
   by usubjid egtestcd ;
run;
proc sql noprint;
	create table base as select usubjid,egtestcd,egstresc as basec, egstresn as base 
	from info where egblfl='Y' order by usubjid,egtestcd;
quit;

data info1;
	merge info(in=a) base;
	by usubjid egtestcd;
run;
/*data intp(keep=usubjid visitnum egstresc egdtc rename=(egstresc=egintpcd)) meas;*/
/*   set info1;*/
/*   if egcat="FINDING" then output intp;*/
/*   else output meas;*/
/*run;*/
/*Proc sort data =intp ;*/
/*   by usubjid visitnum egdtc;*/
/*run;*/
/*Proc sort data =meas ;*/
/*   by usubjid visitnum egdtc;*/
/*run;*/
/*data info2;*/
/*   merge meas intp;*/
/*   by usubjid visitnum egdtc;*/
/*run;*/
data all1;
  format ADT yymmdd10. ATM time5. ADTM DATETIME13.;
  length AVISIT PARAM AVALC /*PARCAT1*/  $200. TRTP TRTA $100.;
  set info1;
  by usubjid;

  *SRCSEQ, SRCDOM;

  SRCSEQ=egseq;
  SRCDOM=domain;
 
  *VISITYP;
  if visitnum^=int(visitnum) then VISITYP="Unscheduled";
  else VISITYP="Scheduled";
  *AVISITN;
  if visitnum ne . then AVISITN=int(visitnum);
  *AVISIT;
  *if avisitn ne . then AVISIT = strip(put(avisitn,tv_vis.));
  avisit=put(avisitn, visitn.);
 if avisit="Early Term" then avisit="Early Term";
 else avisit=avisit;

  *ATPT;
  *if ^missing(egtpt) then ATPT=strip(egtpt);
  *ATPTN;
  *if egtptnum ne . then ATPTN=egtptnum; 

  *PARAM/PARAMN/PARCAT1/PARCAT1N;

  if egtest ne "" and egstresu ne "" then PARAM = strip(egtest) || " ("||strip(egstresu) || ")" ;
  else if egtest ne "" and egstresu=""  then PARAM = strip(egtest) ;
 * if egpos ne "" then PARAM=strip(propcase(egpos))|| " "||strip(param);
parcat1=strip(upcase(egcat));
paramcd=strip(upcase(egtestcd));
if upcase(parcat1)=upcase('finding') then parcat1n=100;
 else if upcase(parcat1)=upcase('measurement') then parcat1n=200;

if upcase(paramcd)=upcase('egvrmn') then paramn=101;
else if upcase(paramcd)=upcase('prag') then paramn=102;
else if upcase(paramcd)=upcase('qrsag') then paramn=103;
else if upcase(paramcd)=upcase('qtag') then paramn=104;
else if upcase(paramcd)=upcase('qtcbag') then paramn=105;
else if upcase(paramcd)=upcase('qtcfag') then paramn=106;
else if upcase(paramcd)=upcase('intp') then paramn=107;
else if upcase(paramcd) ^in(' ' '%upcase(egall)') then put "%upcase(%str(w)arning: paramcd without paramn) " paramcd= paramn=;

  *EGPREC;
  EGPREC=0;

    *AVAL/AVALC;
if paramcd = "INTP" then avalc = strip(egorres);
 else if egstresn ne . then do;
    AVAL=egstresn; 
    AVALC=strip(put(egstresn,8.0)); 
end;

/*if AVALC="ABNORMAL NCS"  then EGCLSIG="NO" ;*/
/*else if AVALC="ABNORMAL CS"  then EGCLSIG="YES" ; */
/*else EGCLSIG=" ";*/

if PARAMCD="INTP" and AVALC="ABNORMAL NCS"  then EGCLSIG="N" ;
else if PARAMCD="INTP" and AVALC="ABNORMAL CS"  then EGCLSIG="Y" ;
else EGCLSIG="";




  *ADT/ATM;
  if ^missing(egdtc) then do;
    ADT=input(substr(egdtc,1,10),yymmdd10.);
    if length(egdtc)>10 then do; ATM=input(substr(egdtc,12,5),time5.); ADTM=input(egdtc, E8601DT.); end;
    ADY=egdy;
   end;

  ABLFL=egblfl;
 
IF ADTM >= TR01SDTM THEN DO; TRTP=TRT01P; TRTPN=TRT01PN;  TRTA=TRT01A; TRTAN=TRT01AN; *APERIOD=TRT01AN; END;
IF ADTM >= TR02SDTM THEN DO; TRTP=TRT02P; TRTPN=TRT02PN;  TRTA=TRT02A; TRTAN=TRT02AN; *APERIOD=TRT02AN; END;
IF ADTM >= TR03SDTM THEN DO; TRTP=TRT03P; TRTPN=TRT03PN;  TRTA=TRT03A; TRTAN=TRT03AN; *APERIOD=TRT03AN; END;
IF ADTM >= TR04SDTM THEN DO; TRTP=TRT04P; TRTPN=TRT04PN;  TRTA=TRT04A; TRTAN=TRT04AN; *APERIOD=TRT04AN; END;
IF ADTM >= TR05SDTM THEN DO; TRTP=TRT05P; TRTPN=TRT05PN;  TRTA=TRT05A; TRTAN=TRT05AN; *APERIOD=TRT05AN; END;
IF ADTM >= TR06SDTM THEN DO; TRTP=TRT06P; TRTPN=TRT06PN;  TRTA=TRT06A; TRTAN=TRT06AN; *APERIOD=TRT06AN; END;
 IF avisitn in (-2,-1,999) then do; TRTP=" "; TRTPN=.; TRTA=""; TRTAN=.;/*APERIOD=.; */end;
 *else if avisitn not in (-2,-1,999) then  aperiod=input(strip(scan(avisit, 2, '')), best.);
/*if aperiod ne . then aperiodc="Period 0"||strip(put(aperiod,best.));*/
/*else aperiodc=" ";*/

  if avisitn > -1 then do;
      if n(aval,base)=2 then do;
         chg=aval-base;
     end;
 end;

 ***ANL01FL***;

if avalc ne " " then ANL01FL="Y";
run;
   


proc freq data =all1 ;
   tables avisitn*avisit/ list missing nocum nopercent;
run;
proc freq data = all1;
   tables paramcd*egstresc*avalc*egclsig/ list missing nocum nopercent;
run;
proc freq data = all1;
   tables visitnum*avisitn*visit/ list missing nocum nopercent;
run;

/****create ANL01FL;*/
/*data miss notmiss;*/
/*   set all1;*/
/*   if avalc ne "" and visitnum=int(visitnum) then output notmiss;*/
/*   else output miss;*/
/*run;*/
/*Proc sort data =notmiss;*/
/*   by USUBJID PARAMN avisitn egdtc;*/
/*run;*/
/*data prior  post;*/
/*set notmiss;*/
/*if ADT>TR01SDT>. or ADTm>TR01SDTm>. then output post;*/
/*else output prior;*/
/*run;*/
/**/
/*Proc sort data = prior ;*/
/*   by USUBJID PARAMN PARAMCD AVISITN  EGDTC;*/
/*run;*/
/**/
/*data _toblfl;*/
/*   length ANL01FL $1.;*/
/*   set prior;*/
/*   by USUBJID PARAMN PARAMCD AVISITN  EGDTC;*/
/*   if last.AVISITN then ANL01FL = 'Y';*/
/*run;*/
/**/
/*Proc sort data = post ;*/
/*   by USUBJID PARAMN PARAMCD AVISITN EGDTC;*/
/*run;*/
/**/
/*data toblfl_;*/
/*   length ANL01FL $1.;*/
/*   set post;*/
/*   by USUBJID PARAMN PARAMCD AVISITN  EGDTC;*/
/*   if first.AVISITN then ANL01FL = 'Y';*/
/*run;*/
/**/
/*data tblfl;*/
/*   set _toblfl  toblfl_;*/
/*   by USUBJID ;  */
/*   if visityp=:"U" then ANL01FL=" ";*/
/*   else if avalc=" " then ANL01FL=" ";*/
/*   else ANL01FL="Y";*/
/*run;*/
/**/
/**/
/*Proc freq data = tblfl;*/
/*   tables avisitn*avisit*visityp*visitnum*anl01fl/ list missing nocum nopercent;*/
/*run;*/
/**/
/*data info;*/
/*   set tblfl miss;* all;*/
/*   by USUBJID;*/
/*run;*/
/*proc sort data=info;*/
/*by usubjid paramn;*/
/*run;*/
/**/
/*data base(keep=usubjid param paramn paramcd aval avalc rename=(aval=base avalc=basec));*/
/*set info;*/
/*if  egblfl eq 'Y';*/
/*proc sort;*/
/*by usubjid paramn;*/
/*run;*/
/*data final1;*/
/*	merge base(in=a) info(in=b);*/
/*	by usubjid paramn;*/
/*	if  b;*/
/*	if egblfl eq "Y" then do;*/
/*	base=.;*/
/*	basec=" ";*/
/*	end;*/
/*	if base ne . and aval ne . then do;*/
/*	CHG=AVAL-BASE;*/
/*    PCHG= ((AVAL-BASE)/BASE)*100;*/
/*	end;*/
/**/
/*run;*/

data final;
  length EGCLSIG $3. usubjid $50.;
  set all1;
  by USUBJID;
  label STUDYID="Study Identifier"
        USUBJID="Unique Subject Identifier"
        SUBJID="Subject Identifier for the Study"
        SITEID="Study Site Identifier"
 
        AVISIT="Analysis Visit"
        AVISITN="Analysis Visit (N)"
        /*ATPT="Analysis Timepoint"
        ATPTN="Analysis Timepoint (N)"*/
        ADT="Analysis Date"
        ATM="Analysis Time"
        ADTM="Analysis Datetime"
        ADY="Analysis Relative Day"
       /* APERIOD="Period"
        APERIODC="Period (C)"*/
        EGPREC="Decimal Precision for Reports"
        PARCAT1="Parameter Category 1"
        PARCAT1N="Parameter Category 1 (N)"
        PARAM="Parameter"
        PARAMCD="Parameter Code"
        PARAMN="Parameter (N)"
        AVAL="Analysis Value"
        AVALC="Analysis Value (C)"
        BASE="Baseline Value"
        BASEC="Baseline Value (C)"
  
        CHG="Change from Baseline"
   
        ABLFL="Baseline Record Flag"
        ANL01FL="Analysis Record Flag 01"
        TRTP="Planned Treatment"
        TRTPN="Planned Treatment (N)"
        TRTA="Actual Treatment"
        TRTAN="Actual Treatment (N)"
        EGCLSIG="Clinically Significance"
        SRCSEQ="Source Sequence Number"
		SRCDOM="Source Data"
        VISITYP="Visit Type"
      ;


run;
/*
proc print data=final;
var subjid parcat1 paramcd param avisitn avisit egclsig;
run;
*/
Proc freq data = FINAL;
   tables paramcd*avisitn*avisit*ablfl/ list missing nocum nopercent;
run;

%*ADSL_varS;
Proc SQL;
   create table adamw.adeg(label="ECG Analysis Dataset" sortedby =STUDYID USUBJID PARAMN AVISITN  compress=yes) as
   SELECT l.STUDYID,l.USUBJID,l.SUBJID,l.SITEID,l.ARMCD,l.ACTARMCD,
          l.ENRLFL,l.SAFFL,l.RANDAFL,l.RANDBFL,l.COMPLFL,l.PKFL,l.PDFL,l.TRTSDT,l.TRTSDTM,l.TRTEDT,l.TRTEDTM,
          r.TRTP,r.TRTPN,r.TRTA,r.TRTAN,/*r.APERIOD,r.APERIODC,*/r.AVISIT,r.AVISITN,r.VISITYP,r.ADT,r.ATM,r.ADTM,r.ADY,
          r.PARCAT1,r.PARCAT1N,r.PARAM,r.PARAMCD,r.PARAMN,
          r.AVAL,r.AVALC,r.BASE,r.BASEC,r.CHG,r.EGPREC,r.ABLFL,r.ANL01FL,r.EGCLSIG,r.EGPOS,r.SRCSEQ,r.SRCDOM
   FROM adam.adsl as l left join final as r
   on l.usubjid = r.usubjid
   order STUDYID, USUBJID, PARAMN, AVISITN
   ;
QUIT;

proc contents data=adam.adeg ;
run;

proc datasets lib=work kill nolist nodetails;
run;


